﻿Imports System
Imports System.IO
Imports System.Text


Public Class ProcessDataForm

    Public Main As Main
    Public Func As Functions = New Functions
    Dim FeatureType As Integer
    Public Property OperationName As String = "N/A"
    Public Property MachiningType As Integer = 0
    '1:フェースミル加工 2:輪郭加工 3:ポケット加工 4:ドリル加工 5:スパイラル加工 6:その他（割り当てられていない加工）
    Public Property ToolID As String = "N/A"
    Public Property SpindleSpeed As Long = 0
    Public Property XYFeedrate As Double = 0
    Public Property ZFeedrate As Double = 0
    Public Property MachiningMode As Integer = 0
    'FaceMill:
    '   1: 一方向  2:双方向
    'PockettMill:
    '   0:一方向   1:Zig-Zag   2:同心-In   3:同心-Out
    'CountourMill:
    '   1:DepthFirst    2:Widthfirst
    'SpiralMill:
    '   0:内から外  1:外から内
    Public Property StepOver As Double = 2
    Public Property TotalDepth As Double = 0
    Public Property IncrementalDepth As Double = 0
    Public Property StartingDepth As Double = 0
    Public Property MachiningStrategy As Integer = 0
    '0:ダウンカット   1:アップカット
    Public Property PassesNum As Integer = 0
    Public Property StockAllowanceWall As Double = 0
    Public Property StockAllowanceFloor As Double = 0

    Dim MaterialXMax, MaterialXMin, MaterialYMax, MaterialYMin, MaterialZMax, MaterialZMin As Double

    Dim FeatureChainKey As Integer
    Dim Point(10, 2) As Double
    Dim MarginXY As Double

    Dim ShapeXMax, ShapeXMin, ShapeYMax, ShapeYMin, ShapeZMax, ShapeZmin As Double
    Dim Depth As Double


    'FeatureChainKeyを受け取って、素材形状/製品形状を読み込み、このフォームを表示
    Public Sub PreLoadForm(ByVal Key As Integer)
        Dim path As String
        Dim Read As StreamReader
        Dim Reader As String
        Dim FeatureNum, FeatureCounter As Integer
        Dim PointNum, PointCounter As Integer
        Dim retval(5) As String
        Dim flag As Boolean = False

        '素材形状を読み込み
        path = "MaterialShape.txt"
        Read = New StreamReader(path)
        Reader = Read.ReadLine
        retval = Reader.Split(CChar(" "))
        MaterialXMax = CDbl(retval(0))
        MaterialYMax = CDbl(retval(1))
        MaterialZMax = CDbl(retval(2))
        MaterialXMin = CDbl(retval(3))
        MaterialYMin = CDbl(retval(4))
        MaterialZMin = CDbl(retval(5))

        '製品形状を読み込み（対象のFeatureChainKeyのみ）
        flag = False
        path = "ShapeDataEsp.txt"
        Read = New StreamReader(path)

        Reader = Read.ReadLine
        retval = Reader.Split(CChar(" "))
        FeatureNum = CInt(retval(0))

        For FeatureCounter = 0 To FeatureNum - 1
            If flag Then Exit For

            Reader = Read.ReadLine
            retval = Reader.Split(CChar(" "))
            FeatureType = CInt(retval(1))

            Reader = Read.ReadLine
            retval = Reader.Split(CChar(" "))
            FeatureChainKey = CInt(retval(0))
            PointNum = CInt(retval(2))

            For PointCounter = 0 To PointNum - 1
                If flag Then Exit For

                Reader = Read.ReadLine
                retval = Reader.Split(CChar(" "))

                '対象のFeatureChain であれば，座標の情報の取得
                If FeatureChainKey = Key Then
                    flag = True
                    Point(PointCounter, 0) = CDbl(retval(0))
                    Point(PointCounter, 1) = CDbl(retval(1))
                    Point(PointCounter, 2) = CDbl(retval(2))
                End If
            Next
            Reader = Read.ReadLine
            retval = Reader.Split(CChar(" "))
            If FeatureChainKey = Key Then
                Depth = CDbl(retval(0))
            End If
        Next

        '製品形状の最大値を設定
        '最初は，0番目の点を取得
        ShapeXMax = Point(0, 0)
        ShapeXMin = Point(0, 0)
        ShapeYMax = Point(0, 1)
        ShapeYMin = Point(0, 1)
        ShapeZMax = Point(0, 2)
        ShapeZmin = Point(0, 2)
        For i As Integer = 1 To PointNum - 1
            If ShapeXMax < Point(i, 0) Then ShapeXMax = Point(i, 0)
            If ShapeXMin > Point(i, 0) Then ShapeXMin = Point(i, 0)
            If ShapeYMax < Point(i, 1) Then ShapeYMax = Point(i, 1)
            If ShapeYMin > Point(i, 1) Then ShapeYMin = Point(i, 1)
            If ShapeZMax < Point(i, 2) Then ShapeZMax = Point(i, 2)
            If ShapeZmin > Point(i, 2) Then ShapeZmin = Point(i, 2)
        Next
        MarginXY = MaterialXMax - ShapeXMax
        If MarginXY < ShapeXMin - MaterialXMin Then MarginXY = ShapeXMin - MaterialXMin
        If MarginXY < MaterialYMax - ShapeYMax Then MarginXY = MaterialYMax - ShapeYMax
        If MarginXY < ShapeYMin - MaterialYMin Then MarginXY = ShapeYMin - MaterialYMin
    End Sub

    'MachiningTypeを見てMachiningModeのリストを設定
    Private Sub ComboBoxMachiningType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxMachiningType.SelectedIndexChanged
        ComboBoxMachiningMode.Items.Clear()
        With ComboBoxMachiningMode
            Select Case ComboBoxMachiningType.SelectedIndex
                Case 1
                    .Items.Add("")
                    .Items.Add("一方向")
                    .Items.Add("双方向")
                    TextBoxPassesNum.Enabled = False
                    Label17.Enabled = False
                Case 2
                    .Items.Add("")
                    .Items.Add("深さ方向から")
                    .Items.Add("幅方向から")
                    TextBoxPassesNum.Enabled = True
                    Label17.Enabled = True
                Case 3
                    .Items.Add("一方向")
                    .Items.Add("ジグザグ")
                    .Items.Add("同心円-In")
                    .Items.Add("同心円-Out")
                    TextBoxPassesNum.Enabled = False
                    Label17.Enabled = False
                Case 5
                    .Items.Add("内から外")
                    .Items.Add("外から内")
                    TextBoxStepOver.Enabled = False
                    Label7.Enabled = False
                    TextBoxPassesNum.Enabled = False
                    Label17.Enabled = False
            End Select
        End With
    End Sub

    'デフォルトの値を設定
    Private Sub ProcessDataForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        StartingDepth = MaterialZMax - ShapeZMax
        PassesNum = CInt(Math.Ceiling(MarginXY / StepOver))

        TextBoxOperationName.Text = OperationName
        ComboBoxMachiningType.SelectedIndex = MachiningType
        TextBoxToolID.Text = ToolID
        TextBoxSpindleSpeed.Text = CStr(SpindleSpeed)
        TextBoxXYFeedrate.Text = CStr(XYFeedrate)
        TextBoxZFeedrate.Text = CStr(ZFeedrate)
        'ComboBoxMachiningMode.SelectedIndex = MachiningMode
        TextBoxStepOver.Text = CStr(StepOver)
        TextBoxTotalDepth.Text = CStr(TotalDepth)
        TextBoxIncrementalDepth.Text = CStr(IncrementalDepth)
        TextBoxStartingDepth.Text = CStr(StartingDepth)
        ComboBoxMachiningStrategy.SelectedIndex = MachiningStrategy
        TextBoxPassesNum.Text = CStr(PassesNum)
    End Sub

    'OKボタンの挙動（フォーム内の値を取得）
    Private Sub ButtonOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOK.Click
        Dim path As String = "RoughProcessData.txt"
        Dim Write As IO.StreamWriter = New IO.StreamWriter(path)

        'フォーム内の値の取得
        MachiningType = ComboBoxMachiningType.SelectedIndex
        OperationName = TextBoxOperationName.Text
        ToolID = TextBoxToolID.Text
        SpindleSpeed = CLng(TextBoxSpindleSpeed.Text)
        XYFeedrate = CDbl(TextBoxXYFeedrate.Text)
        ZFeedrate = CDbl(TextBoxZFeedrate.Text)
        MachiningMode = ComboBoxMachiningMode.SelectedIndex
        StepOver = CDbl(TextBoxStepOver.Text)
        TotalDepth = CDbl(TextBoxTotalDepth.Text)
        IncrementalDepth = CDbl(TextBoxIncrementalDepth.Text)
        StartingDepth = CDbl(TextBoxStartingDepth.Text)
        MachiningStrategy = ComboBoxMachiningStrategy.SelectedIndex
        PassesNum = CInt(TextBoxPassesNum.Text)
        StockAllowanceWall = CDbl(TextBoxStockAllowanceWall.Text)
        StockAllowanceFloor = CDbl(TextBoxStockAllowanceFloor.Text)

        'フォーム内の値の記録
        Write.Write(CStr(Func.Key2Code(FeatureChainKey)) + vbCrLf)
        Write.Write(OperationName + vbCrLf)
        Write.Write(CStr(MachiningType) + vbCrLf)
        Write.Write(ToolID + vbCrLf)
        Write.Write(CStr(SpindleSpeed) + vbCrLf)
        Write.Write(CStr(XYFeedrate) + vbCrLf)
        Write.Write(CStr(ZFeedrate) + vbCrLf)
        Write.Write(CStr(MachiningMode) + vbCrLf)
        Write.Write(CStr(StepOver) + vbCrLf)
        Write.Write(CStr(TotalDepth) + vbCrLf)
        Write.Write(CStr(IncrementalDepth) + vbCrLf)
        Write.Write(CStr(StartingDepth) + vbCrLf)
        Write.Write(CStr(MachiningStrategy) + vbCrLf)
        Write.Write(CStr(PassesNum) + vbCrLf)
        Write.Write(CStr(StockAllowanceWall) + vbCrLf)
        Write.Write(CStr(StockAllowanceFloor) + vbCrLf)

        Me.Close()
    End Sub

    'Cancelボタンの挙動（値を取得せず終了）
    Private Sub ButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCancel.Click
        Me.Close()
    End Sub

    'ダンボーをclickした場合（test用）
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        OperationName = "TEST"
        MachiningType = 1
        ToolID = "EM 04.0"
        SpindleSpeed = 1000
        XYFeedrate = 100
        ZFeedrate = 100
        MachiningMode = 2
        StepOver = 2
        TotalDepth = 5
        IncrementalDepth = 1
        StartingDepth = -5
        MachiningStrategy = 1

        TextBoxOperationName.Text = OperationName
        ComboBoxMachiningType.SelectedIndex = MachiningType
        ComboBoxMachiningMode.Items.Clear()
        With ComboBoxMachiningMode
            Select Case ComboBoxMachiningType.SelectedIndex
                Case 1
                    .Items.Add("")
                    .Items.Add("一方向")
                    .Items.Add("双方向")
                Case 2
                    .Items.Add("")
                    .Items.Add("深さ方向から")
                    .Items.Add("幅方向から")
                Case 3
                    .Items.Add("一方向")
                    .Items.Add("ジグザグ")
                    .Items.Add("同心円-In")
                    .Items.Add("同心円-Out")
            End Select
        End With
        TextBoxToolID.Text = ToolID
        TextBoxSpindleSpeed.Text = CStr(SpindleSpeed)
        TextBoxXYFeedrate.Text = CStr(XYFeedrate)
        TextBoxZFeedrate.Text = CStr(ZFeedrate)
        ComboBoxMachiningMode.SelectedIndex = MachiningMode
        TextBoxStepOver.Text = CStr(StepOver)
        TextBoxTotalDepth.Text = CStr(TotalDepth)
        TextBoxIncrementalDepth.Text = CStr(IncrementalDepth)
        TextBoxStartingDepth.Text = CStr(StartingDepth)
        ComboBoxMachiningStrategy.SelectedIndex = MachiningStrategy

    End Sub

    '計算ボタンの挙動（パス数/深さの計算）
    Private Sub ButtonCalDepth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCalDepth.Click
        '輪郭加工であれば，パス数を計算
        If MachiningType = 2 Then
            PassesNum = CInt(Math.Ceiling(MarginXY / StepOver))
            TextBoxPassesNum.Text = CStr(PassesNum)
        End If
        Dim IsUpside As Boolean

        '各種深さを計算
        IsUpside = Func.IsUpSideOrNot(FeatureType)
        If IsUpside Then
            StartingDepth = (MaterialZMax - ShapeZMax) + Depth
            StartingDepth = -StartingDepth
            TotalDepth = 0
        Else
            StartingDepth = MaterialZMax - ShapeZMax
            StartingDepth = -StartingDepth
            TotalDepth = Depth
        End If
        TextBoxStartingDepth.Text = CStr(StartingDepth)
        TextBoxTotalDepth.Text = CStr(TotalDepth)
    End Sub
End Class